$(document).ready   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 37
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 54
rs 8.5253

3 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
A 0 26 2
A 0 7 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
$(document).ready(function () {
2
    function getCurrentDateStr() {
3
        var month = d.getMonth() + 1;
4
        var day = d.getDate();
5
        return d.getFullYear() + '-' +
6
            (month < 10 ? '0' : '') + month + '-' +
7
            (day < 10 ? '0' : '') + day;
8
    }
9
10
    function getCurrentTimeStr() {
11
        return d.getHours() + ":" + d.getMinutes();
12
    }
13
14
    function scrollToNearReport() {
15
        var scrollTo = null;
16
        var current_report = null;
17
        var prev_report = null;
18
        var now_time = getCurrentTimeStr();
19
20
        $('.program-body__td--time').each(function (index, value) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter value is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
21
            if (current_report !== null) {
22
                prev_report = current_report;
23
            }
24
            current_report = $(this);
25
            if (now_time < current_report.text()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if now_time < current_report.text() is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
26
                if (prev_report !== null) {
27
                    scrollTo = prev_report;
28
                } else {
29
                    scrollTo = current_report;
30
                }
31
                return false;
32
            }
33
        });
34
        if (scrollTo !== null) {
35
            $('body,html').animate({
36
                scrollTop: scrollTo.offset().top - 125
37
            }, 600);
38
        }
39
    }
40
41
    var event_header_date_element = $('.event-header__date');
42
    if (event_header_date_element.length) {
43
        var d = new Date();
44
        var now_date = getCurrentDateStr();
45
        var event_date = event_header_date_element.attr('datetime');
46
47
        if ('scrollRestoration' in history) {
0 ignored issues
show
Bug introduced by
The variable history seems to be never declared. If this is a global, consider adding a /** global: history */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
48
            history.scrollRestoration = event_date === now_date ? 'manual' : 'auto';
49
        }
50
        if (event_date === now_date) {
51
            scrollToNearReport();
52
        }
53
    }
54
});